<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="cfn" uri="http://francetelecom.com/cfn"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/fn"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@page import="org.springframework.validation.Errors"%>

<%-- Creates a form period selector. Parameters are :
    - code : the label code for the input
    - path : the path for the input
    - focused : optional, value must be "focused" to focus the input
    - mandatory : optional, boolean string value, 'true' if the field is mandatory.
    - disabled : optional, boolean string value, 'true' if the field is disabled.
    - usePartialValue : optional, boolean string value, 'true' if the related label info box
      must display a label notifying the user that he can use partial values.
    - dateTime : whether the period chooser should be a date time one
--%>

<%-- Whether a time chooser should be displayed --%>
<c:set var="showTime" value="${param.dateTime == 'true'}"/>

<jsp:include page="form-field-header.jsp">
  <jsp:param name="code" value="${param.code}"/>
  <jsp:param name="mandatory" value="${param.mandatory}"/>
  <jsp:param name="usePartialValue" value="${param.usePartialValue}"/>
</jsp:include>

<%-- Whether the fields should be disabled --%>
<c:set var="disabled" value="${param.disabled == 'true' ? true : false}"/>

<%-- Displaying the form field --%>
<span class="date${showTime? 'Time' : ''}">

  <%-- The min date field --%>
  <span><spring:theme code='form.date.from' />&nbsp;</span>
  <c:set var="fieldPath" value="${param.path}.min"/>
  <c:set var="dtId" value="${fn:replace(fn:replace(fn:replace(param.path, '.', '_'), '[', '_'), ']', '_')}"/>
  <c:set var="minDtId" value="${dtId}_min"/>

  <form:input path="${fieldPath}" size="20" cssClass="${focused}" id="${minDtId}" disabled="${disabled}"/>

  <%-- The max date field --%>
  <span>&nbsp;<spring:theme code='form.date.to' />&nbsp;</span>
  <c:set var="fieldPath" value="${param.path}.max"/>
  <c:set var="maxDtId" value="${dtId}_max"/>

  <form:input path="${fieldPath}" size="20" id="${maxDtId}" cssErrorClass="error" disabled="${disabled}"/>
</span>

<%-- Displaying the form field error message --%>
<jsp:include page="form-field-errors.jsp">
  <jsp:param name="path" value="${param.path}"/>
</jsp:include>

<jsp:include page="form-field-footer.jsp"/>

<!-- Setting date/time picker global params -->
<c:if test="${not disabled}">

  <script type="text/javascript">
    $(document).ready(function() {

        var language = '${pageContext.response.locale.language}';
        language = language == 'en' ? '' : language;
        var reg = $.datepicker.regional[language];
        $.datepicker.setDefaults(reg);
        var inputIds = ['${minDtId}', '${maxDtId}'];

        for(var i = 0; i < inputIds.length; i++) {

            var options = {
                dateFormat : '${datePickerPattern}'

                <c:if test="${showTime == true}">
                  ,
                  timeFormat : '${timePickerPattern}',
                  ampm:${fn:contains(timePickerPattern, 'T') ? 'true' : 'false'},
                  hour: (i==0 ? 0 : 23),
                  minute: (i==0 ? 0 : 59),
                  showSecond:false,
                  timeText: '<spring:theme code="time"/>',
                  hourText: '<spring:theme code="time.hour"/>',
                  minuteText: '<spring:theme code="time.minute"/>',
                  currentText: '<spring:theme code="time.now"/>'
                </c:if>
            };

        	$("#" + inputIds[i]).date${showTime ? 'time' : ''}picker(options);
        }
    });
  </script>
</c:if>